# -*- coding: utf-8 -*-
import requests
r1=requests.get('http://www.baidu.com')

#输出所有cookie
print(r1.cookies)

##获取每一个cookie
#for key,value in r1.cookies.items():
#    print(key,'=',value)
#
##获取简述首页
#headers={
#     'Host':'www.jianshu.com',
#     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36',
#     'Cookie':'UM_distinctid=180117224dd1c5-0d2245c64010ed-9771a3f-100200-180117224de791; CNZZDATA1279807957=218291970-1649557025-https%253A%252F%252Fwww.baidu.com%252F%7C1649557025; sajssdk_2015_cross_new_user=1; remember_user_token=W1syNzYyMzI4M10sIiQyYSQxMSRMdnpXWk1OTmFUakxwOUlFTU5odUxlIiwiMTY0OTU2MDE2OC4yOTU3OTA3Il0%3D--acc339de035f3bf92e887dbc2f494d5c99dda3e1; web_login_version=MTY0OTU2MDE2OA%3D%3D--870179e92453fbdfe81cffa76a21abced1e59dc5; _m7e_session_core=300760f5dc575ec7c06c1ad3a9df6291; _ga=GA1.2.1464810712.1649561039; _gid=GA1.2.768049668.1649561039; read_mode=day; default_font=font2; locale=zh-CN; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%2227623283%22%2C%22first_id%22%3A%2218011722546e60-007db60f693c9b-9771a3f-1049088-18011722547b17%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E8%87%AA%E7%84%B6%E6%90%9C%E7%B4%A2%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC%22%2C%22%24latest_referrer%22%3A%22https%3A%2F%2Fwww.baidu.com%2Flink%22%7D%2C%22%24device_id%22%3A%2218011722546e60-007db60f693c9b-9771a3f-1049088-18011722547b17%22%7D; Hm_lvt_0c0e9d9b1e7d617b3e6842e85b9fb068=1649560135,1649561120; Hm_lpvt_0c0e9d9b1e7d617b3e6842e85b9fb068=1649561120; BAIDU_SSP_lcr=https://www.baidu.com/link?url=1Ik36il1QlMtTweU2bfr44viTkpvkrfV4scO8Vc9OievhHj0KN8Uaflo6Rf0dzJ9&wd=&eqid=e775f2ed000028d90000000662524e17',
#     }
#
##请求简述首页，并通过headers参数发送Cookie
#r2=requests.get('https://www.jianshu.com',headers=headers)
#print(r2.text)   

#另外一种Cookie设置
headers={
     'Host':'www.jianshu.com',
     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36',
    }
cookies='UM_distinctid=180117224dd1c5-0d2245c64010ed-9771a3f-100200-180117224de791; CNZZDATA1279807957=218291970-1649557025-https%253A%252F%252Fwww.baidu.com%252F%7C1649557025; sajssdk_2015_cross_new_user=1; remember_user_token=W1syNzYyMzI4M10sIiQyYSQxMSRMdnpXWk1OTmFUakxwOUlFTU5odUxlIiwiMTY0OTU2MDE2OC4yOTU3OTA3Il0%3D--acc339de035f3bf92e887dbc2f494d5c99dda3e1; web_login_version=MTY0OTU2MDE2OA%3D%3D--870179e92453fbdfe81cffa76a21abced1e59dc5; _m7e_session_core=300760f5dc575ec7c06c1ad3a9df6291; _ga=GA1.2.1464810712.1649561039; _gid=GA1.2.768049668.1649561039; read_mode=day; default_font=font2; locale=zh-CN; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%2227623283%22%2C%22first_id%22%3A%2218011722546e60-007db60f693c9b-9771a3f-1049088-18011722547b17%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E8%87%AA%E7%84%B6%E6%90%9C%E7%B4%A2%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC%22%2C%22%24latest_referrer%22%3A%22https%3A%2F%2Fwww.baidu.com%2Flink%22%7D%2C%22%24device_id%22%3A%2218011722546e60-007db60f693c9b-9771a3f-1049088-18011722547b17%22%7D; Hm_lvt_0c0e9d9b1e7d617b3e6842e85b9fb068=1649560135,1649561120; Hm_lpvt_0c0e9d9b1e7d617b3e6842e85b9fb068=1649561120; BAIDU_SSP_lcr=https://www.baidu.com/link?url=1Ik36il1QlMtTweU2bfr44viTkpvkrfV4scO8Vc9OievhHj0KN8Uaflo6Rf0dzJ9&wd=&eqid=e775f2ed000028d90000000662524e17'
jar=requests.cookies.RequestsCookieJar()

for cookie in cookies.split(';'):
    key,value=cookie.split('=',1)
    #将其添加到对象当中
    jar.set(key,value)

#请求简述首页，并通过cookies参数发生Cookie
r3=requests.get('http://www.jianshu.com',cookies=jar,headers=headers)
print(r3.text)
     
